package com.mall507.fivepeaks.activity.find;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.BaseSP;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.core.ImageFrame.BaseImage;
import com.hzh.frame.ui.fragment.BaseFM;
import com.hzh.frame.util.StatusBarUtil;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.xgridview.XGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类首页
 * */
public class TypeIndexFM extends BaseFM {

    public static final int RETURN_TYPE = 0xff02;
	private ListView fListView,sListView;
	private List<HashMap<String, Object>> fList,sList;
	private View mView;
	
	@Override
	protected void onCreateBase() {
        mView=setContentView(R.layout.ui_store_type);
        getTitleView().addView(StatusBarUtil.createStatusBarView(getActivity(), ContextCompat.getColor(getActivity(), R.color.application_color)), 0);
        getTitleView().setLeftIsShow(false);
        getTitleView().setContent("发现");
		fListView= mView.findViewById(R.id.fListView);
		sListView= mView.findViewById(R.id.sListView);
		fListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
		sListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
		loadNetworkData(0,"","");
	}

	/**
	 * 加载分类数据
	 * @param type 0:加载一级分类数据  1:加载二级分类数据
	 * */
	private void loadNetworkData(int type,String level,String goodsType_id){
		if(type==0){
			//加载一级分类数据
			BaseHttp.getInstance().query("", null, new HttpCallBack(){
				@Override
				public void onSuccess(JSONObject response) {
					if(1==response.optInt("result")){
						JSONObject data=response.optJSONObject("data");
						if(1==data.optInt("code")){
							JSONArray list=data.optJSONArray("goodsClass");
							fList=new ArrayList();
							if(list!=null && list.length()>0){
								for (int i = 0; i < list.length(); i++) {
									JSONObject model=list.optJSONObject(i);
									HashMap<String, Object> map=new HashMap<String, Object>();
									map.put("name",model.optString("className") );
									map.put("id",model.optString("goodsType_id") );
									map.put("level",model.optString("level") );
									fList.add(map);
								}
                                sListView.setBackgroundColor(getResources().getColor(R.color.white));
                                loadNetworkData(1,fList.get(0).get("level").toString(),fList.get(0).get("id").toString());
							}else{
								fListView.setBackgroundColor(getResources().getColor(R.color.comn_bg));
							}
							fListView.setAdapter(new fListViewAdapter(getContext(), fList, R.layout.item_lv_store_type_f, new String[]{"name"}, new int[]{R.id.content}));
						}
						dismissLoding();
					}else{
						showLodingFail();
					}
				}
				
				@Override
				public void onFail() {
					showLodingFail();
				}
			});
		}else{
			//加载二级分类数据
			JSONObject params=new JSONObject();
			try {
				params.put("level", level);
				params.put("goodsType_id", goodsType_id);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			BaseHttp.getInstance().query("", params, new HttpCallBack(){
				@Override
				public void onSuccess(JSONObject response) {
					if(1==response.optInt("result")){
						JSONObject data=response.optJSONObject("data");
						if(1==data.optInt("code")){
							JSONArray listClass=data.optJSONArray("goodsClass");
							if(listClass!=null && listClass.length()>0){
								sList=new ArrayList<HashMap<String, Object>>();
								for (int i = 0; i < listClass.length(); i++) {
									JSONObject obj=listClass.optJSONObject(i);
									HashMap<String, Object> map=new HashMap<String, Object>();
									map.put("typeName",obj.optString("name"));
									JSONArray list=obj.optJSONArray("goodsClassList");
									if(list!=null && list.length()>0){
										List<HashMap<String, String>> listModel=new ArrayList<HashMap<String,String>>();
										for (int j = 0; j < list.length(); j++) {
											JSONObject model=list.optJSONObject(j);
											HashMap<String, String> mapModel=new HashMap<String, String>();
											mapModel.put("name",model.optString("className"));
											mapModel.put("id",model.optString("goodsType_id"));
											mapModel.put("path",model.optString("photoUrl"));
											listModel.add(mapModel);
										}
										map.put("items", listModel);
									}
									sList.add(map);
								}
							}
							if(!Util.isEmpty(sList)){
                                sListView.setAdapter(new sListViewAdapter(getContext(), sList, R.layout.item_lv_store_type_s, new String[]{"typeName"}, new int[]{R.id.typeName}));
							}else{
							    fListView.setVisibility(View.GONE);
                                sListView.setVisibility(View.GONE);
                                mView.findViewById(R.id.recyclerViewBg).setVisibility(View.VISIBLE);
                            }
						}
					}
				}
				
			});
		}
	}
	
	/**
	 * 一级分类适配器
	 */
	private class fListViewAdapter extends SimpleAdapter{
		
		private int index=0;//当前被选中的项

		public fListViewAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View v=super.getView(position, convertView, parent);
			TextView content=(TextView) v.findViewById(R.id.content);
			if(position==index){
				v.setBackgroundColor(getResources().getColor(R.color.comn_bg));
				content.setTextColor(getResources().getColor(R.color.application_color));
			}else{
				v.setBackgroundColor(getResources().getColor(R.color.white));
				content.setTextColor(Color.parseColor("#666666"));
			}
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					index=position;
					notifyDataSetChanged();
					loadNetworkData(1,fList.get(position).get("level").toString(),fList.get(position).get("id").toString());
				}
			});
			return v;
		}
	}
	/**
	 * 二级分类适配器
	 */
	private class sListViewAdapter extends SimpleAdapter{
		public sListViewAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View v=super.getView(position, convertView, parent);
			XGridView gridView= v.findViewById(R.id.gview);
			List<HashMap<String, String>> resourceList=(List<HashMap<String, String>>)sList.get(position).get("items");
			if(resourceList !=null && resourceList.size()>0){
				gridView.setVisibility(View.VISIBLE);
				if(!Util.isEmpty(resourceList.get(0).get("path"))){
					gridView.setBackgroundResource(R.color.white);
				}else{
					gridView.setBackgroundResource(R.color.comn_bg);
				}
				gridView.setAdapter(new sGridViewAdapter(getContext(), resourceList, R.layout.item_gv_store_type_s, new String[]{"name"}, new int[]{R.id.name}));
			}else{
				gridView.setVisibility(View.GONE);
			}
			return v;
		}
	}
	
	/**
	 * 二级分类九宫格适配器
	 */
	private class sGridViewAdapter extends SimpleAdapter{
		private List<HashMap<String, String>> resourceList;
		
		public sGridViewAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[] from, int[] to) {
			super(context, data, resource, from, to);
			resourceList=(List<HashMap<String, String>>) data;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View v=super.getView(position, convertView, parent);
			if(!Util.isEmpty(resourceList.get(position).get("path"))){
				v.findViewById(R.id.image).setVisibility(View.VISIBLE);
                BaseImage.getInstance().loadAutoWidth( 
                        (SimpleDraweeView) (v.findViewById(R.id.image)),
                        resourceList.get(position).get("path"),
                        Util.dip2px(getContext(),50));
			}else{
				v.findViewById(R.id.image).setVisibility(View.GONE);
			}
			return v;
		}
	}
}
